﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FairyGUI;

namespace Model
{
    [ObjectEvent]
    public class PlayerUIComponentEvent : ObjectEvent<PlayerUIComponent>, IAwake<GComponent,GComponent>
    {
        public void Awake(GComponent gComponent, GComponent MianView)
        {
            Get().Awake(gComponent, MianView);
            Get().Awake();
        }
    }

    public class PlayerUIComponent : BattleEventComponent
    {
        public override BattleEventType[] EventType { get; set; } = new BattleEventType[] { BattleEventType.FreshUI };
        BattlePlayer battleplayer;
        GComponent Main, MainView;
        BattleView battleView;
        public void Awake(GComponent gComponent, GComponent MianView)
        {
            battleplayer = GetEntity<BattlePlayer>();
            Main = gComponent;
            this.MainView = MianView;
            
            Main.GetChild("hand").asList.onClickItem.Add(ListClick);
            Main.GetChild("power").asList.onClickItem.Add(ListClick);
            Main.GetChild("battle").asList.onClickItem.Add(ListClick);            
            battleView = FGuiComponent.Single.GetView(UIViewType.Battle) as BattleView;
            Main.GetChild("n6").onClick.Add(() => {
                BattleCard card = null;
                for (int i = 0; i < battleplayer[CardPlace.Charactor].Cards.Count; i++)
                {
                    if (battleplayer[CardPlace.Charactor].Cards[i].IfSupport)
                    {
                        card = battleplayer[CardPlace.Charactor].Cards[i];
                        break;
                    }
                    battleView.SetChooseCard(card, Main.GetChild("n6").asCom);
                }
            });
            Main.GetChild("n7").onClick.Add(() => {
                BattleCard card = null;
                for (int i = 0; i < battleplayer[CardPlace.Charactor].Cards.Count; i++)
                {
                    if (battleplayer[CardPlace.Charactor].Cards[i].IfCenter)
                    {
                        card = battleplayer[CardPlace.Charactor].Cards[i];
                        break;
                    }
                    battleView.SetChooseCard(card, Main.GetChild("n7").asCom);
                }
            });
        }
        public void ListClick(EventContext eventContext)
        {
            battleView.SetChooseCard((GComponent)eventContext.data);
        }
        public void Fresh()
        {
            Main.GetChild("n9").text = battleplayer.Hp.ToString();
            Main.GetChild("n12").asProgress.value = battleplayer.Hp * 100 / 10000;
            GList HandList = Main.GetChild("hand").asList;
            for (int i = 0; i < HandList.numChildren; i++)
            {
                if (i < battleplayer[CardPlace.Hand].Cards.Count)
                    BattleCard.SetCardToUI(HandList.GetChildAt(i).asCom, battleplayer[CardPlace.Hand].Cards[i]);
                else
                    BattleCard.SetCardToUI(HandList.GetChildAt(i).asCom, null);
            }
            GList PowerList = Main.GetChild("power").asList;
            for (int i = 0; i < PowerList.numChildren; i++)
            {
                if (i < battleplayer[CardPlace.Power].Cards.Count)
                    BattleCard.SetCardToUI(PowerList.GetChildAt(i).asCom, battleplayer[CardPlace.Power].Cards[i]);
                else
                    BattleCard.SetCardToUI(PowerList.GetChildAt(i).asCom, null);
            }
            GList BattleList = Main.GetChild("battle").asList;
            for (int i = 0; i < BattleList.numChildren; i++)
            {
                if (i < battleplayer[CardPlace.SpellChain].Cards.Count)
                    BattleCard.SetCardToUI(BattleList.GetChildAt(i).asCom, battleplayer[CardPlace.SpellChain].Cards[i]);
                else
                    BattleCard.SetCardToUI(BattleList.GetChildAt(i).asCom, null);
            }
            BattleCard.SetCardToUI(Main.GetChild("n7").asCom, null);
            BattleCard.SetCardToUI(Main.GetChild("n6").asCom, null);
            var CharactorList = battleplayer[CardPlace.Charactor].Cards;
            bool first = true;
            for (int i = 0; i < CharactorList.Count; i++)
            {
                if (CharactorList[i].IfStand) BattleCard.SetCardToUI(Main.GetChild("n7").asCom, CharactorList[i]);
                if (!CharactorList[i].IfStand && first)
                {
                    BattleCard.SetCardToUI(Main.GetChild("n6").asCom, CharactorList[i]);
                    first = false;
                }
            }
            GList powerList = Main.GetChild("n11").asList;
            for (int i = 0; i < powerList.numChildren; i++)
            {
                GComponent item = powerList.GetChildAt(i).asCom;
                if (i < battleplayer.Power)
                    item.GetController("c1").selectedIndex = 0;
                else if (i < battleplayer.MaxPower - battleplayer.BrokenPower)
                    item.GetController("c1").selectedIndex = 1;
                else
                    item.GetController("c1").selectedIndex = 2;
            }
        }

        public override async Task RunEvent(BattleEventType eventType, FightModel fightModel, params object[] param)
        {           
            Fresh();
        }
    }
}
